﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBUtility;
using System.Data;


namespace OracleDAL.Sec
{
    /// <summary>
    /// 数据访问类UserNode。
    /// </summary>
    public class UserNode
    {
        public UserNode()
        { }
        #region  成员方法


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string USERISN)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SEC_USER_NODE");
            strSql.Append(" where USERISN='" + USERISN + "' ");
            return OracleHelper.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.Sec.UserNode model)
        {   
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.USERISN != null)
            {
                strSql1.Append("USERISN,");
                strSql2.Append("'" + model.USERISN + "',");
            }
            if (model.NODEID != null)
            {
                strSql1.Append("NODEID,");
                strSql2.Append("'" + model.NODEID + "',");
            }
            if (model.NODEISN != null)
            {
                strSql1.Append("NODEISN,");
                strSql2.Append("'" + model.NODEISN + "',");
            }
            strSql.Append("insert into SEC_USER_NODE(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            int row = OracleHelper.ExecuteSql(strSql.ToString());
            if (row > 0)
                return true;
            else return false;
        }
        /// <summary>
        /// 给单位下所有用户增加资源范围权限
        /// </summary>
        /// <param name="NodeID">节点ID</param>
        /// <param name="NodeISN">节点ISN</param>
        /// <param name="UnitISN">单位ISN</param>
        /// <returns></returns>
        public bool Add(string NodeID, string NodeISN,string UnitISN)
        {
            string sql = "insert into SEC_USER_NODE select userisn,'"+NodeID+"','"+NodeISN+"' from sec_userinfo_new where unitisn='" + UnitISN + "'";
            return OracleHelper.ExecuteSql(sql) > 0 ? true : false;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.Sec.UserNode model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SEC_USER_NODE set ");
            strSql.Append("NODEID='" + model.NODEID + "',");
            strSql.Append("NODEISN='" + model.NODEISN + "'");
            strSql.Append(" where USERISN='" + model.USERISN + "' ");
            int row = OracleHelper.ExecuteSql(strSql.ToString());
            if (row > 0)
                return true;
            else return false;
        }
        /// <summary>
        /// 更新单位下所有用户资源范围权限
        /// </summary>
        /// <param name="NodeID">节点ID</param>
        /// <param name="NodeISN">节点ISN</param>
        /// <param name="UnitISN">单位ISN</param>
        /// <returns></returns>
        public bool Update(string NodeID, string NodeISN, string UnitISN)
        {
            string sql = "update SEC_USER_NODE set nodeid='"+NodeID+"',nodeisn='"+NodeISN+"' where userisn in (select userisn from sec_userinfo_new where unitisn='" + UnitISN + "')";
            return OracleHelper.ExecuteSql(sql) > 0 ? true : false;
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string USERISN)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete SEC_USER_NODE ");
            strSql.Append(" where USERISN='" + USERISN + "' ");
            int row = OracleHelper.ExecuteSql(strSql.ToString());
            if (row > 0)
                return true;
            else return false;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Sec.UserNode GetModel(string USERISN)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  ");
            strSql.Append(" USERISN,NODEID,NODEISN ");
            strSql.Append(" from SEC_USER_NODE ");
            strSql.Append(" where USERISN='" + USERISN + "' ");
            Model.Sec.UserNode model = new Model.Sec.UserNode();
            DataSet ds = OracleHelper.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.USERISN = ds.Tables[0].Rows[0]["USERISN"].ToString();
                model.NODEID = ds.Tables[0].Rows[0]["NODEID"].ToString();
                model.NODEISN = ds.Tables[0].Rows[0]["NODEISN"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select USERISN,NODEID,NODEISN ");
            strSql.Append(" FROM SEC_USER_NODE ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return OracleHelper.Query(strSql.ToString());
        }
        #endregion  成员方法
    }
}
